package com.niannian.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.niannian.BaseActivity;
import com.niannian.MyApplication;
import com.niannian.R;
import com.niannian.activity.AddDateTipActivity;
import com.niannian.activity.AddFamilyMemberActivity;
import com.niannian.activity.FamilyMemberActivity;
import com.niannian.activity.FamilyMemberDetailActivity;
import com.niannian.activity.MyInfoEditActivity;
import com.niannian.bean.MyFamilyMemberBean;
import com.niannian.db.MyDBUser;
import com.niannian.dialog.DeleteMenberDialog;
import com.niannian.dialog.DeleteMenberDialog.onSimpleOnMListener;
import com.niannian.dialog.YesNoDialog2;
import com.niannian.dialog.YesNoDialog2.onSimpleOnCancelListener;
import com.niannian.dialog.YesNoDialog2.onSimpleOnClicklListener;
import com.niannian.util.AsyncHandle;
import com.niannian.util.Common;
import com.niannian.util.ErrorCode;
import com.niannian.util.UmengPage;
import com.niannian.util.UserInfoManager;
import com.niannian.view.CircleImageView;
import com.niannian.web.nnSyncApi;
import com.umeng.analytics.MobclickAgent;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class FamilyMemberAdaper extends BaseAdapter {
	protected MyDBUser userInfoManager = UserInfoManager.getUserInfoInstance();
	private ArrayList<MyFamilyMemberBean> fdatas;
	private LayoutInflater inflater;
	private Activity mActivity;
	private int ACTION_TYPE = 0;
	Handler nHandler;

	public FamilyMemberAdaper(Activity activity, int ACTION_TYPE, Handler nHandler) {
		super();
		this.mActivity = activity;
		this.ACTION_TYPE = ACTION_TYPE;
		this.nHandler = nHandler;
		inflater = LayoutInflater.from(mActivity);

		this.fdatas = new ArrayList<MyFamilyMemberBean>();
	}

	@Override
	public int getCount() {
		return fdatas.size() + 1;
	}

	@Override
	public Object getItem(int arg0) {
		return fdatas.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	public ArrayList<MyFamilyMemberBean> getFDatasList() {
		return fdatas;
	}

	@Override
	public View getView(final int position, View view, ViewGroup arg2) {
		ViewHolder holder = null;
		if (view == null) {
			view = inflater.inflate(R.layout.item_family_member, null);
			holder = new ViewHolder();
			holder.ll_family_item = (LinearLayout) view.findViewById(R.id.ll_family_item);
			holder.my_family_head = (CircleImageView) view.findViewById(R.id.my_family_head);
			holder.my_family_head_P = (CircleImageView) view.findViewById(R.id.my_family_head_P);

			int pich = (int) ((BaseActivity.mScreenWidth / 3) - Common.dip2px(mActivity, 30));
			LayoutParams params = holder.my_family_head.getLayoutParams();
			params.height = pich;
			params.width = pich;
			holder.my_family_head.setLayoutParams(params);
			holder.my_family_head_P.setLayoutParams(params);

			holder.tv_nickname = (TextView) view.findViewById(R.id.tv_nickname);

			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}

		if (position == 0) {

			holder.tv_nickname.setTextColor(mActivity.getResources().getColor(R.color.col_4));
			holder.my_family_head.setImageResource(R.drawable.add_family_member);
			if (ACTION_TYPE == 0) {
				holder.tv_nickname.setText("添加亲友");
			} else if (ACTION_TYPE == 1) {
				holder.tv_nickname.setText("从手机选择");
			}

			holder.my_family_head_P.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					if (ACTION_TYPE == 1) {
						nHandler.sendEmptyMessage(2);
					} else {
						Intent intent = new Intent(mActivity, AddFamilyMemberActivity.class);

						intent.putExtra("ACTION_TYPE", ACTION_TYPE);
						mActivity.startActivityForResult(intent, FamilyMemberActivity.MEMBER_ADD);

						mActivity.overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);

						MobclickAgent.onEvent(mActivity, UmengPage.click_add_friend);
					}

				}
			});

		} else {
			final MyFamilyMemberBean fdata = fdatas.get(position - 1);
			holder.tv_nickname.setTextColor(mActivity.getResources().getColor(R.color.col_2));
			MyApplication.finalbitmap.display(holder.my_family_head, Common.getAvatarThumbs(fdata.getAvatar()));

			if (fdata.getNickname().length() > 0) {
				holder.tv_nickname.setText(fdata.getNickname());
			} else {
				holder.tv_nickname.setText(fdata.getPhone() + "");
			}

			if (ACTION_TYPE == 0) {
				holder.my_family_head_P.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						if (userInfoManager.id == fdata.getId()) {
							Intent intent = new Intent(mActivity, MyInfoEditActivity.class);
							mActivity.startActivity(intent);
							mActivity.overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
						} else {

							Intent intent = new Intent(mActivity, FamilyMemberDetailActivity.class);
							intent.putExtra("position", position - 1);
							intent.putExtra("data", fdata);
							mActivity.startActivityForResult(intent, FamilyMemberActivity.MEMBER_DETAIL);
							mActivity.overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
						}
					}
				});

				if (userInfoManager.family_perm == 1 && userInfoManager.id != fdata.getId()) {
					holder.my_family_head_P.setOnLongClickListener(new OnLongClickListener() {

						@Override
						public boolean onLongClick(View v) {
							DeleteMenberDialog deleteMember = new DeleteMenberDialog(mActivity);
							deleteMember.setOnMListener(new onSimpleOnMListener() {
								@Override
								public void onM() {
									initDialog(fdata.getId(), position);
								}
							});
							deleteMember.show();
							return true;
						}
					});
				} else {
					holder.my_family_head_P.setOnLongClickListener(null);
				}

			} else if (ACTION_TYPE == 1) {
				holder.my_family_head_P.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View arg0) {

						Intent intent = new Intent();
						int from = 1;// 表示来自通讯录
						intent.putExtra("from", from);
						intent.putExtra("uid", fdata.getId());
						intent.putExtra("avatar", fdata.getAvatar());
						mActivity.setResult(mActivity.RESULT_OK, intent);
						mActivity.finish();
						mActivity.overridePendingTransition(R.anim.push_right_in2, R.anim.push_right_to2);
					}
				});
			}

		}
		holder.ll_family_item.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

			}
		});
		return view;
	}

	int uid = 0;

	class ViewHolder {
		LinearLayout ll_family_item;
		CircleImageView my_family_head;
		CircleImageView my_family_head_P;
		TextView tv_nickname;

	}

	YesNoDialog2 tip;

	private void initDialog(final int currUid, final int position) {
		tip = new YesNoDialog2(mActivity, "确认删除该成员?");
		tip.setMsg2("注意: 成员被删除后, 将无法再与家庭成员互动.");
		tip.setcancelButtonText("取  消");
		tip.setConfimButtonText("删  除");
		tip.setOnCancelListener(new onSimpleOnCancelListener() {

			@Override
			public void onCancel() {
				tip.dismiss();
			}
		});
		tip.setOnConfimClickListener(new onSimpleOnClicklListener() {

			@Override
			public void onClick() {
				Map<String, Object> paramMap = new HashMap<String, Object>();
				paramMap.put("currUid", currUid);
				paramMap.put("position", position);
				new deleteFamilyMenmber().init(mActivity, paramMap, true, "删除中...").execute();
				tip.dismiss();
			}
		});
		tip.show();
	}

	protected class deleteFamilyMenmber extends AsyncHandle {

		protected void errorFinally(Map<String, Object> paramMap) {
			Common.tip(mActivity, "删除成员失败");
		}

		protected void handleData(JSONObject paramJSONObject, Map<String, Object> paramMap) throws JSONException {
			try {
				int retCode = paramJSONObject.getInt("code");
				if (retCode == ErrorCode.OK) {
					int currUid = Integer.parseInt(paramMap.get("currUid") + "");

					for (int i = 0; i < fdatas.size(); i++) {
						if (fdatas.get(i).getId() == currUid) {
							fdatas.remove(i);
							notifyDataSetChanged();
							break;
						}
					}
					Common.tip(mActivity, "删除成员成功");
					return;

				} else {
					Common.tip(mActivity, paramJSONObject.getString("msg"));
				}
			} catch (Exception e) {
				// TODO: handle exception
			}
		}

		protected String runTask(Map<String, Object> paramMap) {
			return nnSyncApi.deleteFamilyMenber(Integer.parseInt(paramMap.get("currUid") + ""), 1);
		}
	}

}
